<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/9/20
 * Time: 12:05
 */
namespace Happy\Controller;
use Common\Controller\AdminbaseController;
//开心一刻 后台
class IndexController extends AdminbaseController
{
    //主页
    public function index()
    {
        $count      = M('happy')->order('id desc')->count();
        $Page       = new \Think\Page($count,10);
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();
        $list = M('happy')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }
    //添加文章
    public function add()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['name']))) {
                $this->error('请填写文章标题');
            }
            if(trim(empty($post['thumb']))) {
                $this->error('请上传文章封面图');
            }
            $data = [
                'name'=>$post['name'],
                'auth'=>$post['auth'],
                'thumb'=>'http://'.$_SERVER[HTTP_HOST].'/data/upload/'.$post['thumb'],
                'type'=>$post['type'],
                'content'=>htmlspecialchars_decode($post['content']),
                'read'=>0,
                'url'=>$post['url'],
                'laiyuanurl'=>$post['laiyuanurl'],
                'time'=>date("Y-m-d H:i",time()),
                'laiyuan'=>$post['laiyuan']
            ];
            if(M('happy')->add($data)){
                $this->success('添加成功！');
            }else{
                $this->error('失败！未知错误！');
            }
        }else{
            $this->display();
        }
    }
    /*楼盘删除*/
    public function del()
    {
        $id = i('get.id');
        if(M('happy')->where(['id'=>$id])->delete()){
            $this->success('删除成功!~');
        }else{
            $this->error('失败，未知错误');
        }
    }
    /*批量删除*/
    public function delall()
    {
        $post = i('post.');
        if(IS_POST) {
            foreach ($post['ids'] as $k) {
                M('happy')->where(['id' => $k])->delete();
            }
            $this->redirect(U('Index/index'));
        }
    }
    /*搜索*/
    public function ser()
    {
        $ser = i('post.ser');
        $where= array('like','%'.$ser.'%');
        if(IS_POST){
            if(trim(empty($ser))){
                $this->error('查询内容不能为空');
            }
            $count      = M('happy')->where(['name'=>$where])->count();
            if($count==0){
                $this->error('没有找到你要找的内容,抱歉了。');
            }
            $Page       = new \Think\Page($count,10);
            $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show       = $Page->show();
            $list = M('happy')->where(['name'=>$where])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

            $this->assign(['page'=>$show,'data'=>$list]);
            $this->display();
        }else{
            $this->display();
        }
    }
    /*置顶*/
    public function sort()
    {
        $id = i('get.id');
        $data = M('happy')->where(['id'=>$id])->find();
        $save = $data['sort']+299;
        if(M('happy')->where(['id'=>$id])->save(['sort'=>$save])){
            $this->success('ok');
        }else{
            $this->error('fail');
        }
    }
}